import request from '@/utils/request'

// 排行榜相关类型定义
export interface UserRanking {
  id: number
  username: string
  avatar?: string
  title?: string
  score: number
  solved: number
  submissions: number
  acceptance: number
  last_active: string
  rank: number
  level: number
  level_name: string
  weekly_score?: number
  monthly_score?: number
  yearly_score?: number
}

export interface RankingStats {
  total_users: number
  active_users: number
  total_submissions: number
  total_solved: number
  average_score: number
  average_acceptance: number
}

// API请求参数类型
export interface GetRankingParams {
  page?: number
  page_size?: number
  type?: 'score' | 'solved' | 'contests'
  period?: 'all' | 'weekly' | 'monthly' | 'yearly'
  search?: string
}

export interface GetTopUsersParams {
  limit?: number
}

// API响应类型
export interface RankingListResponse {
  code: number
  data: {
    items: UserRanking[]
    total: number
    page: number
    page_size: number
    total_pages: number
    type: string
    period: string
  }
  message: string
}

// 排行榜API类
export class RankingAPI {
  // 获取排行榜
  static async getRanking(params?: GetRankingParams): Promise<RankingListResponse> {
    const response = await request.get('/v1/ranking', { params })
    return response.data
  }

  // 获取排行榜统计信息
  static async getRankingStats(): Promise<{ code: number; data: RankingStats; message: string }> {
    const response = await request.get('/v1/ranking/stats')
    return response.data
  }

  // 获取前N名用户
  static async getTopUsers(params?: GetTopUsersParams): Promise<UserRanking[]> {
    const response = await request.get('/v1/ranking/top', { params })
    return response.data
  }

  // 获取用户排名
  static async getUserRank(userId: number): Promise<UserRanking> {
    const response = await request.get(`/v1/ranking/user/${userId}`)
    return response.data
  }
}

// 排行榜类型显示名称映射
export const RankingTypeNames: Record<string, string> = {
  score: '积分排行',
  solved: '解题数量',
  contests: '竞赛排名'
}

// 时间段显示名称映射
export const PeriodNames: Record<string, string> = {
  all: '全部时间',
  weekly: '本周',
  monthly: '本月',
  yearly: '本年'
}

// 等级显示名称映射
export const LevelNames: Record<number, string> = {
  1: '新手',
  2: '入门',
  3: '初级',
  4: '中级',
  5: '高级',
  6: '专家',
  7: '大师',
  8: '宗师',
  9: '传奇',
  10: '神话'
}

// 工具函数
export const getRankingTypeDisplayName = (type: string): string => {
  return RankingTypeNames[type] || '积分排行'
}

export const getPeriodDisplayName = (period: string): string => {
  return PeriodNames[period] || '全部时间'
}

export const getLevelDisplayName = (level: number): string => {
  return LevelNames[level] || '新手'
}

export const formatTime = (dateString: string): string => {
  const date = new Date(dateString)
  const now = new Date()
  const diff = now.getTime() - date.getTime()
  
  if (diff < 1000 * 60) {
    return '刚刚'
  } else if (diff < 1000 * 60 * 60) {
    return `${Math.floor(diff / (1000 * 60))}分钟前`
  } else if (diff < 1000 * 60 * 60 * 24) {
    return `${Math.floor(diff / (1000 * 60 * 60))}小时前`
  } else if (diff < 1000 * 60 * 60 * 24 * 7) {
    return `${Math.floor(diff / (1000 * 60 * 60 * 24))}天前`
  } else if (diff < 1000 * 60 * 60 * 24 * 30) {
    return `${Math.floor(diff / (1000 * 60 * 60 * 24 * 7))}周前`
  } else if (diff < 1000 * 60 * 60 * 24 * 365) {
    return `${Math.floor(diff / (1000 * 60 * 60 * 24 * 30))}个月前`
  } else {
    return `${Math.floor(diff / (1000 * 60 * 60 * 24 * 365))}年前`
  }
}

export const getRankIcon = (rank: number): string => {
  if (rank === 1) return '🥇'
  if (rank === 2) return '🥈'
  if (rank === 3) return '🥉'
  return `#${rank}`
}

export const getRankColor = (rank: number): string => {
  if (rank === 1) return 'text-yellow-500'
  if (rank === 2) return 'text-gray-400'
  if (rank === 3) return 'text-orange-500'
  return 'text-gray-600'
}

export const getLevelColor = (level: number): string => {
  if (level <= 2) return 'text-gray-500'
  if (level <= 4) return 'text-green-500'
  if (level <= 6) return 'text-blue-500'
  if (level <= 8) return 'text-purple-500'
  return 'text-red-500'
}
